/**
 * 数据校验
 */
class ValidatorFn {
    /**
     *
     * @desc   判断是否为16进制颜色，rgb 或 rgba
     * @param  {String}  str
     * @return {Boolean}
     */
    static isColor(str) {
        return /^(#([0-9a-fA-F]{3}){1,2}|[rR][gG][Bb](\((\s*(2[0-4]\d|25[0-5]|[01]?\d{1,2})\s*,){2}\s*(2[0-4]\d|25[0-5]|[01]?\d{1,2})\s*\)|[Aa]\((\s*(2[0-4]\d|25[0-5]|[01]?\d{1,2})\s*,){3}\s*([01]|0\.\d+)\s*\)))$/.test(str);
    }

    /**
     * @desc   判断是否为邮箱地址
     * @param  {String}  str
     * @return {Boolean}
     */
    static isEmail(str) {
        return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(str);
    }

    /**
     * @desc   判断是否为网址
     * @param  {String}  str
     * @return {Boolean}
     */
    static isUrl(str) {
        return /^((\w+):\/\/)?([A-z0-9]+[_\-]?[A-z0-9]+\.)*[A-z0-9]+\-?[A-z0-9]+\.[A-z]{2,}(\/.*)*\/?$/.test(str);
    }

    /**
     * @desc   判断是否为国际号码
     * @param  {String}  str
     * @return {Boolean}
     */
    static isGlobalMobile(str) {
        return /^[\+\-0-9a]+$/.test(str);
    }

    /**
     * @desc   判断是否为手机号码
     * @param  {String}  str
     * @return {Boolean}
     */
    static isMobile(str) {
        return /^1\d{10}$/.test(str);
    }

    /**
     * @desc   判断是否为电话号码
     * @param  {String}  str
     * @return {Boolean}
     */
    static isTel(str) {
        return /(^(\+\d{2,3}\/)?\d{3,4}(-)?\d{7,8}(\*\d{2,6})?$)|(^1\d{10}$)/.test(str);
    }

    /**
     * @desc   判断是否为数字
     * @param  {String}  str
     * @return {Boolean}
     */
    static isNumber(str) {
        return !isNaN(Number(value));
    }

    /**
     * @desc   判断是否为整数
     * @param  {String}  str
     * @return {Boolean}
     */
    static isInt(str) {
        return parseInt(value, 10) === value;
    }

    /**
     * @desc   判断是否为身份证号
     * @param  {String|Number} str
     * @return {Boolean}
     */
    static isIdCard(str) {
        return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(str)
    }

    /**
     * @desc   判断是否为严格的身份证号
     * @param  {String|Number} str
     * @return {Boolean}
     */
    static isStrongIdCard(sId) {
        if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
            return false
        }
        //身份证城市
        var aCity = {
            11: "北京",
            12: "天津",
            13: "河北",
            14: "山西",
            15: "内蒙古",
            21: "辽宁",
            22: "吉林",
            23: "黑龙江",
            31: "上海",
            32: "江苏",
            33: "浙江",
            34: "安徽",
            35: "福建",
            36: "江西",
            37: "山东",
            41: "河南",
            42: "湖北",
            43: "湖南",
            44: "广东",
            45: "广西",
            46: "海南",
            50: "重庆",
            51: "四川",
            52: "贵州",
            53: "云南",
            54: "西藏",
            61: "陕西",
            62: "甘肃",
            63: "青海",
            64: "宁夏",
            65: "新疆",
            71: "台湾",
            81: "香港",
            82: "澳门",
            91: "国外"
        };
        if (!aCity[parseInt(sId.substr(0, 2))]) {
            return false
        }
        // 出生日期验证
        var sBirthday = (sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2))).replace(/-/g, "/"),
            d = new Date(sBirthday)
        if (sBirthday !== (d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate())) {
            return false
        }
        // 身份证号码校验
        var sum = 0,
            weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
            codes = "10X98765432";
        for (var i = 0; i < sId.length - 1; i++) {
            sum += sId[i] * weights[i];
        }
        var last = codes[sum % 11]; //计算出来的最后一位身份证号码
        if (sId[sId.length - 1] !== last) {
            return false
        }
        return true
    }

    /**
     * @desc   判断浏览器是否支持webP格式图片
     * @return {Boolean}
     */
    static isSupportWebP() {
        return !![].map && document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') === 0;
    }

    /**
     * @desc   判断当前设备是否为IOS
     * @returns {boolean}
     */
    static isIOS() {
        var u = navigator.userAgent;
        if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机
            return false
        } else if (u.indexOf('iPhone') > -1) {//苹果手机
            return true
        } else if (u.indexOf('iPad') > -1) {//iPad
            return false
        } else if (u.indexOf('Windows Phone') > -1) {//winphone手机
            return false
        } else {
            return false
        }
    }

    /**
     * @desc    判断当前设备是否为PC端
     * @returns {boolean}
     */
    static isPC() {
        var userAgentInfo = navigator.userAgent;
        var Agents = ["Android", "iPhone",
            "SymbianOS", "Windows Phone",
            "iPad", "iPod"];
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    /**
     * @desc    检测密码强度
     * @param   {String} str 密码
     * @returns {number} 强度值
     */
    static checkPwd(str) {
        var Lv = 0;
        if (str.length < 6) {
            return Lv
        }
        if (/[0-9]/.test(str)) {
            Lv++
        }
        if (/[a-z]/.test(str)) {
            Lv++
        }
        if (/[A-Z]/.test(str)) {
            Lv++
        }
        if (/[\.|-|_]/.test(str)) {
            Lv++
        }
        return Lv;
    }
}

module.exports = ValidatorFn;